package view;

import handler.MainViewHandler;
import handler.StudentMainViewHandler;
import req.StudentGradeRequest;
import req.StudentRequest;
import res.StudentDTO;
import res.TableDTO;
import service.StudentGradeService;
import service.StudentLoginService;
import service.StudentService;
import service.impl.StudentGradeServiceImpI;
import service.impl.StudentServiceImpI;
import util.DimensionUtil;
import view.ext.MainViewTable;
import view.ext.MainViewTableModel;
import view.ext.StudentMainViewTable;
import view.ext.StudentViewTableModel;

import javax.swing.*;
import java.awt.*;

public class StudentMainView extends JFrame {

    JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBTn = new JButton("查询");

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton preBtn = new JButton("上一页");
    JButton nextBtn = new JButton("下一页");


    //    MainViewTable mainViewTable = new MainViewTable();
    StudentMainViewTable studentMainViewTable = new StudentMainViewTable();

    //MainViewHandler mainViewHandler;
    StudentMainViewHandler studentMainViewHandler;

    private int pageNow = 1;//第几页
    private int pageSize = 10;//一页十条

    public StudentMainView() {
        super("学生成绩查询");
        studentMainViewHandler = new StudentMainViewHandler(this);
        //studentMainViewTable.renderRule();
        Container contentPane = getContentPane();
        //北边组件
        layoutNorth(contentPane);

        //中间组件
        layoutCenter(contentPane);

        //放置南边的组件
        layoutSouth(contentPane);

        //JTable


        //加入图片
        //URL imgurl = MainView.class.getClassLoader().getResource("小图标.jpg");
        //setIconImage(new ImageIcon(imgurl).getImage());
        //自适应屏幕大小
        setBounds(DimensionUtil.getBounds());
        //设置窗口完全充满整个屏幕的可见大小
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        //setSize(600,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }

    public static void main(String[] args) {
        new StudentMainView();

    }

    private void layoutCenter(Container contentPane) {
        //StudentLoginService s
        //Vector<Vector<Object>> data = getVectors();
//        TableDTO dto = getTableDTO();
        StudentDTO dto = new StudentDTO();
        //MainViewTableModel mainViewTableModel = MainViewTableModel.assembleModel(dto.getData());
        StudentViewTableModel studentViewTableModel = StudentViewTableModel.assembleModel(dto.getData());
        //Jtable 和 model关联
        studentMainViewTable.setModel(studentViewTableModel);
        studentMainViewTable.renderRule();
        JScrollPane jScrollPane = new JScrollPane(studentMainViewTable);
        contentPane.add(jScrollPane, BorderLayout.CENTER);
        showPreNext(dto.getTotalCount());
    }

    private StudentDTO getStudentDTO() {
        StudentGradeService studentGradeService = new StudentGradeServiceImpI();
        StudentGradeRequest request = new StudentGradeRequest();
        request.setPageNow(pageNow);
        request.setPageSize(pageSize);
        request.setSearchKey(searchTxt.getText().trim());
        StudentDTO studentDTO = studentGradeService.retrieveStudent(request);
        //Vector<Vector<Object>> data = tableDTO.getData();
        return studentDTO;
    }

    private void layoutSouth(Container contentPane) {
        preBtn.addActionListener(studentMainViewHandler);
        nextBtn.addActionListener(studentMainViewHandler);
        southPanel.add(preBtn);
        southPanel.add(nextBtn);
        contentPane.add(southPanel, BorderLayout.SOUTH);
    }

    //上一页下一页按钮可见
    private void showPreNext(int totalCount) {
        if (pageNow == 1) {
            preBtn.setVisible(false);
        } else {
            preBtn.setVisible(true);
        }
        int pageCount = 0;
        if (totalCount % pageSize == 0) {
            pageCount = totalCount / pageSize;
        } else {
            pageCount = totalCount / pageSize + 1;
        }
        if (pageNow == pageCount) {
            nextBtn.setVisible(false);
        } else {
            nextBtn.setVisible(true);
        }

    }

    private void layoutNorth(Container contentPane) {
        //增加事件监听
        addBtn.addActionListener(studentMainViewHandler);
        updateBtn.addActionListener(studentMainViewHandler);
        delBtn.addActionListener(studentMainViewHandler);
        searchBTn.addActionListener(studentMainViewHandler);
//
//        northPanel.add(addBtn);
//        northPanel.add(updateBtn);
//        northPanel.add(delBtn);

        northPanel.add(searchTxt);
        northPanel.add(searchBTn);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public void reloadTable() {
        System.out.println(123);
        StudentDTO dto = getStudentDTO();
        StudentViewTableModel.updateModel(dto.getData());
        studentMainViewTable.renderRule();
        showPreNext(dto.getTotalCount());
    }


    //获取选中的行
//    public int[] getSelectStudentIds() {
//        int[] selectRows = mainViewTable.getSelectedRows();
//        int[] ids = new int[selectRows.length];
//        for (int i = 0; i < selectRows.length; i++) {
//            int row = selectRows[i];
//            Object idObj = mainViewTable.getValueAt(row, 0);
//            ids[i] = Integer.valueOf(idObj.toString());
//        }
//        return ids;
//    }
}
